package org.xxpay.pay.channel.hmapay;

import org.apache.commons.lang3.StringUtils;
import org.xxpay.core.common.util.MD5Util;
import org.xxpay.core.common.util.MyLog;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;


/**
 * 
 *<p>工具类</p>
 * @author  NOT-FIND
 * @version 2018年7月19日 NOT-FIND
 */
public class HmaUtil {
	private static final MyLog _log = MyLog.getLog(HmaUtil.class);

	/**
	 * 创建md5摘要,规则:按参数名称a-z排序,遇到空的参数不参加签名
	 * 
	 * @param packageParams
	 * @return
	 */
	public static String createSign( Map<String, Object> packageParams, String partnerKey) {
		StringBuffer sb = new StringBuffer();
		Set es = packageParams.entrySet();
		Iterator it = es.iterator();
		String sign = "";
		while (it.hasNext()) {
			Map.Entry entry = (Map.Entry) it.next();
			String k = (String) entry.getKey();
			String v = (String) entry.getValue();
			if (StringUtils.isNotEmpty(v) && !"signData".equals(k)) {
				sb.append(k + "=" + v + "&");
			}
		}
		sign = sb.toString() + "key=" + partnerKey;
		try {
			_log.info("待签名串：{}", sign);
			sign = MD5Util.string2MD5(sign);
			_log.info("签名结果:{}", sign);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return sign;
	}

}
